import os

import requests
from fake_useragent import UserAgent

save_directory = 'tsLib'
if not os.path.exists(save_directory):
    os.mkdir(save_directory)

url = "https://v9.dious.cc/20230908/5gr57sIS/2000kb/hls/index.m3u8"
headers = {"User-Agent": UserAgent().random}
res_list = requests.get(url=url, headers=headers).text

lines = res_list.split('\n')
desired_data = [line for line in lines if line.startswith('/') and line.endswith('.ts')]

print(desired_data)

domain = 'https://v9.dious.cc'

for item in desired_data:
    url = domain + item
    response = requests.get(url)
    if response.status_code == 200:
        # 构造保存路径
        file_path = os.path.join(save_directory, item.split('/')[-1])
        # 保存文件
        with open(file_path, 'wb') as file:
            file.write(response.content)
        print(f"下载成功：{url}")
    else:
        print(f"下载失败：{url}")
